/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/
package world.ah.wf.guiclient;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
//import javax.swing.UIManager;

public class WfGuiClient {
  public static String connPropertiesFileName = "";
  public WfGuiClient() { 
    JFrame frame = new MainForm();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
  }

  /**
   * 
   * @param args
   */
  public static void main(String[] args) {
    /*
    // Установка своего потока вывода 
    PrintStream out;
    try {
      out = new PrintStream(new FileOutputStream("wfout.log"), true, "UTF-16");
      System.setOut(out);
    } catch (Exception ex) {System.out.println("Cannot set custom output stream!");}
    // Установка своего потока ошибок
    PrintStream err;
    try {
      err = new PrintStream(new FileOutputStream("wferr.log"), true, "UTF-16");
      System.setErr(err);
    } catch (Exception ex) {System.out.println("Cannot set custom error stream!");}
    // установили
    */
    //
    for (int i = 0; i < args.length; i++) System.out.println(args[i]);
    // Разбор параметров 
    int st = 0;
    for(int i = 0; i < args.length; i++) {
      if(st == 0) 
        if(args[i].equalsIgnoreCase("-CONN")) st = 1;
        else {
          ErrorShower.show(null, "Wrong argument (#" + Integer.toString(i) + 
            "): -CONN was expected");
          break;
        }
      else if(st == 1)
        if(args[i].length() > 0) {
          connPropertiesFileName = args[i];
          st = 2;
        } 
        else {
          ErrorShower.show(null, "Wrong argument (#" + Integer.toString(i) + 
            "): properties file name was expected");
          break;
        }
      else if(st > 1) {
        ErrorShower.show(null, "Wrong argument (#" + Integer.toString(i) + 
        "): no more parameter was expected");
          break;
        }
    }
    if(st == 1) ErrorShower.show(null, "Wrong arguments: properties file name was expected");
    // Обработка параметров программы
    OptionsHolder opt = new OptionsHolder();
    General.optionsHolder = opt;
    opt.setPropertiesFileName("wfOptions.properties");
    try {opt.loadProperties();} 
    catch (Exception ex) {
      ErrorShower.show(null, "загрузка параметров программы из файла", "", ex);
    }
    // Установка внешного вида
    General.setLookAndFeelFromOptions();
    // Язык
    try {General.loadLanguageFile(opt.getLanguageFileName());} 
    catch (Exception ex) {ErrorShower.show(null, "загрузка файла языка", "", ex);}
 
    
    //
    new WfGuiClient();
  }
}